from panda3d.core import Vec3, VBase3

class WebProjectile():

    def __init__(self, name, damage, has_flighttime, speed, pos, target, manager):
        self.name = name
        self.damage = damage
        self.has_flighttime = has_flighttime
        self.speed = speed
        self.target = target
        self.target_pos = target.pos
        self.manager = manager
        self.pos = pos

    def update(self, dt):
        if self.target is not None:
            self.target_pos = Vec3(self.target.pos.x, self.target.pos.y, 0)
        if not self.target_reached():
            self.move(dt)

    def move(self, dt):
        dir = self.target.pos - self.pos
        VBase3.normalize(dir)
        self.pos = self.pos + (dir * self.speed * dt)

    def target_reached(self):
        dis = self.target.pos - self.pos
        leng = VBase3.length(dis)
        if leng < 0.2:
            if self.target is not None:
                self.target.damage(self.damage)
            self.manager.destroy_projectile(self)
            return True
        return False

